package com.ruoyi.saas.strategy.impl;



import com.baidu.fsg.uid.UidGenerator;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.context.ContextUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataScopeType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.saas.dto.TenantConnectDTO;
import com.ruoyi.saas.strategy.InitSystemStrategy;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 初始化规则:
 * 手动创建数据
 *
 * @author zuihou
 * @date 2020年04月05日13:14:28
 */
@Service("COLUMN")
@Slf4j
@RequiredArgsConstructor
public class ColumnInitSystemStrategy implements InitSystemStrategy {
    private static final String TOPIC = "topic";
    private static final String EVENTLIST = "eventList";
    private static final String REPORTTEMPLATE = "reportTemplate";


    private static final String CASELIST = "caselist";
    private static final String CASEQYERY = "caseqyery";
    private static final String CASEADD = "caseadd";
    private static final String CASEEDIT = "caseedit";
    private static final String CASEDELETE = "casedelete";

    private static final String TOPICLISTID = "topiclistid";
    private static final String TOPICQUERYID = "topicqueryid";
    private static final String TOPICDELETEID = "topicdeleteid";
    private static final String TOPICADDID = "topicaddid";
    private static final String TOPICEDITID = "topiceditid";

    private static final String RULELISTID = "rulelistid";
    private static final String RULEADDID = "ruleaddid";
    private static final String RULEEDITID = "ruleeditid";
    private static final String RULEQUERYID = "rulequeryid";
    private static final String RULEREMOVEID = "ruleremoveid";

    private static final String RECEIVERLISTID = "receiverlistid";
    private static final String RECEIVERADDID = "receiveraddid";
    private static final String RECEIVEREDITID = "receivereditid";
    private static final String RECEIVERQUERYID = "receiverqueryid";
    private static final String RECEIVERREMOVEID = "receiverremoveid";

    private static final String REPORTMANAGEID = "reportmanageid";

    private static final String FORECASTLISTID = "forecastlistid";
    private static final String FORECASTRECEIVERID = "forecastReceiverId";
    private static final String FORECASTRULEID = "forecastruleid";

    private static final String REPORTLISTID = "reportlistid";
    private static final String REPORTQUERYID = "reportqueryid";
    private static final String REPORTADDID = "reportaddid";
    private static final String REPORTEDITID = "reporteditid";
    private static final String REPORTREMOVEID = "reportremoveid";

    private static final String TEMPLATELISTID = "templatelistid";
    private static final String TEMPLATEQUERYID = "templatequeryid";
    private static final String TEMPLATEADDID = "templateaddid";
    private static final String TEMPLATEEDITID = "templateeditid";
    private static final String TEMPLATEREMOVEID = "templateremoveid";
    private static final String EVENTMANAGEID = "eventmanageid";
    private static final String EVENTLISTID = "eventlistid";
    private static final String EVENTQUERYID = "eventqueryid";
    private static final String EVENTDELETEID = "eventdeleteid";
    private static final String EVENTADDID = "eventaddid";
    private static final String EVENTEDITID = "eventeditid";
    private static final String USERMANAGEID = "usermanageid";
    private static final String ROLEMANAGEID = "rolemanageid";
    private static final String DEPTMANAGEID = "deptmanageid";
    private static final String POSTMANAGEID = "postmanageid";
    private static final String NOTICEMANAGEID = "noticemanageid";

    private static final String FILELIST = "filelist";
    private static final String FILEQYERY = "fileqyery";
    private static final String FILEADD = "fileadd";
    private static final String FILEEDIT = "fileedit";
    private static final String FILEDELETE = "filedelete";

    private static final String USERLIST = "userlist";
    private static final String USERQYERY = "userqyery";
    private static final String USERADD = "useradd";
    private static final String USEREDIT = "useredit";
    private static final String USERDELETE = "userdelete";
    private static final String USERRESETPWDID = "userresetpwdid";

    private static final String ROLELIST = "ROLELIST";
    private static final String ROLEQYERY = "ROLEQYERY";
    private static final String ROLEADD = "ROLEADD";
    private static final String ROLEEDIT = "ROLEEDIT";
    private static final String ROLEDELETE = "ROLEDELETE";

    private static final String DEPTLIST = "DEPTLIST";
    private static final String DEPTQYERY = "DEPTQYERY";
    private static final String DEPTADD = "DEPTADD";
    private static final String DEPTEDIT = "DEPTEDIT";
    private static final String DEPTDELETE = "DEPTDELETE";

    private static final String POSTLIST = "POSTLIST";
    private static final String POSTQYERY = "POSTQYERY";
    private static final String POSTADD = "POSTADD";
    private static final String POSTEDIT = "POSTEDIT";
    private static final String POSTDELETE = "POSTDELETE";

    private static final String NOTICELIST = "NOTICELIST";
    private static final String NOTICEQYERY = "NOTICEQYERY";
    private static final String NOTICEADD = "NOTICEADD";
    private static final String NOTICEEDIT = "NOTICEEDIT";
    private static final String NOTICEDELETE = "NOTICEDELETE";


    private final ISysMenuService menuService;
    private final ISysRoleService roleService;
    @Qualifier("cachedUidGenerator")
    private final UidGenerator uidGenerator;
    private final ISysUserService userService;
    private final ISysDeptService deptService;
    private final ISysPostService postService;
    private final ISysNoticeService noticeService;


    /**
     * 我*，这种方式太脑残了，但想不出更好的方式初始化数据，希望各位大神有好的初始化方法记得跟我说声！！！
     * 写这段代码写得想去si ~~~
     * <p>
     * 不能用 SCHEMA 模式的初始化脚本方法： 因为id 会重复，租户编码会重复！
     *
     * @param tenantConnect 待初始化租户编码
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean initConnect(TenantConnectDTO tenantConnect) {
        String tenant = tenantConnect.getTenant();
        // 初始化数据
        //1, 生成并关联 ID TENANT
        ContextUtil.setTenant(tenant);

        // 菜单 资源 角色 角色_资源 字典 参数
        List<SysMenu> menuList = new ArrayList<>();
        Map<String, Long> menuMap = new HashMap<>();
        boolean menuFlag = initMenu(menuList, menuMap);

        // 角色
        Long roleId = uidGenerator.getUID();
        boolean roleFlag = initRole(roleId,tenant);

        //角色菜单绑定
        List<SysRoleMenu> resourceList = new ArrayList<>();
        boolean resourceFlag = initResource(resourceList, roleId,menuList,tenant);

        //部门
        Long deptId = uidGenerator.getUID();
        boolean deptFlag= initDept(deptId,tenantConnect.getName(),tenant);

        // 内置超级管理员
        initSuperUser(tenant,deptId);
        return menuFlag && roleFlag && resourceFlag && deptFlag;

    }

    private boolean initDept(Long deptId, String name, String tenant) {
        SysDept user = SysDept.builder().deptId(deptId).deptName(name).tenantCode(tenant).parentId(0L).ancestors("0").build();
        return deptService.initDept(user)>0;
    }


    private boolean initSuperUser(String tenant, Long deptId) {
       SysUser user = SysUser.builder()
                .userName(tenant+"Admin")
                .nickName("内置超级管理员")
                .password(SecurityUtils.encryptPassword("admin123"))
                .readonly(true)
                .sex("1")
                .status("0")
                .deptId(deptId)
                .tenantCode(tenant)
                .build();
        return userService.insertAdminUser(user)>0;
    }

    private boolean initRole(Long roleId,String tenant) {
        SysRole role = SysRole.builder().roleId(roleId).roleName("超级管理员").roleKey("SUPER_ADMIN").remark("内置超级管理员").dataScope(DataScopeType.ALL.getCode()).readonly(true).status("0").tenantCode(tenant).build();
        return roleService.insertRoleOnly(role)>0;
    }

    private boolean initResource(List<SysRoleMenu> resourceList, Long roleId,List<SysMenu> menuList,String tenant) {
        for (int i = 0; i <menuList.size() ; i++) {
            SysMenu menu=menuList.get(i);
            resourceList.add(SysRoleMenu.builder().roleId(roleId).menuId(menu.getMenuId()).tenantCode(tenant).build());
        }
        return roleService.saveSysRoleMenuBatch(resourceList);
    }

    private boolean initMenu(List<SysMenu> menuList, Map<String, Long> menuMap) {

        Long monitoringId = uidGenerator.getUID();
        Long eventId = uidGenerator.getUID();
        Long reportId = uidGenerator.getUID();
        Long forecastId = uidGenerator.getUID();
        Long searchId = uidGenerator.getUID();
        Long caseId = uidGenerator.getUID();
        Long favoriteId = uidGenerator.getUID();
        Long datavId = uidGenerator.getUID();
        Long systemManageId = uidGenerator.getUID();

        // 1级菜单
//        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(dataImportId).menuName("采集管理").path("dataManagement").component(null).icon("druid").orderNum(String.valueOf(10)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(monitoringId).menuName("舆情监测").path("monitoring").component(null).icon("监测2").orderNum(String.valueOf(20)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(eventId).menuName("舆情事件分析").path("events").component(null).icon("dashboard").orderNum(String.valueOf(30)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(reportId).menuName("舆情简报").path("report").component(null).icon("documentation").orderNum(String.valueOf(40)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(forecastId).menuName("舆情预警").path("forecast").component(null).icon("time-range").orderNum(String.valueOf(50)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(searchId).menuName("云搜索").path("search/index/redirect").component(null).icon("search").orderNum(String.valueOf(60)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(caseId).menuName("案例库").path("case/index/redirect").component(null).icon("icon-case").orderNum(String.valueOf(70)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(favoriteId).menuName("收藏夹").path("favorite/index/redirect").component(null).icon("icon-favorites").orderNum(String.valueOf(80)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(datavId).menuName("数据大屏").path("datav/index/redirect").component(null).icon("icon-data-visualization").orderNum(String.valueOf(90)).readonly(true).build());
        menuList.add(SysMenu.builder().menuType("M").parentId(0L).menuId(systemManageId).menuName("系统管理").path("system").component(null).icon("system").orderNum(String.valueOf(110)).readonly(true).build());

        //用户管理
        Long userManageId = uidGenerator.getUID();
        menuMap.put(USERMANAGEID, userManageId);
        menuList.add(SysMenu.builder().menuType("C").menuId(userManageId).parentId(systemManageId).menuName("用户管理").path("user").component("system/user/index").orderNum(String.valueOf(10)).readonly(true).build());

        Long userListId = uidGenerator.getUID();
        menuMap.put(USERLIST, userListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(userListId).parentId(userManageId).menuName("用户列表").orderNum(String.valueOf(10)).perms("system:user:list").readonly(true).build());
        Long userQueryId = uidGenerator.getUID();
        menuMap.put(USERQYERY, userQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(userQueryId).parentId(userManageId).menuName("用户查询").orderNum(String.valueOf(10)).perms("system:user:query").readonly(true).build());
        Long userAddId = uidGenerator.getUID();
        menuMap.put(USERADD, userAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(userAddId).parentId(userManageId).menuName("用户增加").orderNum(String.valueOf(10)).perms("system:user:add").readonly(true).build());
        Long userEditId = uidGenerator.getUID();
        menuMap.put(USEREDIT, userEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(userEditId).parentId(userManageId).menuName("用户编辑").orderNum(String.valueOf(10)).perms("system:user:edit").readonly(true).build());
        Long userDeleteId = uidGenerator.getUID();
        menuMap.put(USERDELETE, userDeleteId);
        menuList.add(SysMenu.builder().menuType("F").menuId(userDeleteId).parentId(userManageId).menuName("用户删除").orderNum(String.valueOf(10)).perms("system:user:remove").readonly(true).build());
        Long userResetPwdId = uidGenerator.getUID();
        menuMap.put(USERRESETPWDID, userResetPwdId);
        menuList.add(SysMenu.builder().menuType("F").menuId(userResetPwdId).parentId(userManageId).menuName("重置密码").orderNum(String.valueOf(10)).perms("system:user:resetPwd").readonly(true).build());



        //角色管理
        Long roleManageId = uidGenerator.getUID();
        menuMap.put(ROLEMANAGEID, roleManageId);
        menuList.add(SysMenu.builder().menuType("C").menuId(roleManageId).parentId(systemManageId).menuName("角色管理").path("role").component("system/role/index").orderNum(String.valueOf(20)).readonly(true).build());
        Long roleListId = uidGenerator.getUID();
        menuMap.put(ROLELIST, roleListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(roleListId).parentId(roleManageId).menuName("角色列表").orderNum(String.valueOf(10)).perms("system:role:list").readonly(true).build());
        Long roleQueryId = uidGenerator.getUID();
        menuMap.put(ROLEQYERY, roleQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(roleQueryId).parentId(roleManageId).menuName("角色查询").orderNum(String.valueOf(10)).perms("system:role:query").readonly(true).build());
        Long roleAddId = uidGenerator.getUID();
        menuMap.put(ROLEADD, roleAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(roleAddId).parentId(roleManageId).menuName("角色增加").orderNum(String.valueOf(10)).perms("system:role:add").readonly(true).build());
        Long roleEditId = uidGenerator.getUID();
        menuMap.put(ROLEEDIT, roleEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(roleEditId).parentId(roleManageId).menuName("角色编辑").orderNum(String.valueOf(10)).perms("system:role:edit").readonly(true).build());
        Long roleDeleteId = uidGenerator.getUID();
        menuMap.put(ROLEDELETE, roleDeleteId);
        menuList.add(SysMenu.builder().menuType("F").menuId(roleDeleteId).parentId(roleManageId).menuName("角色删除").orderNum(String.valueOf(10)).perms("system:role:remove").readonly(true).build());


        //部门管理
        Long deptManageId = uidGenerator.getUID();
        menuMap.put(DEPTMANAGEID, deptManageId);
        menuList.add(SysMenu.builder().menuType("C").menuId(deptManageId).parentId(systemManageId).menuName("部门管理").path("dept").component("system/dept/index").orderNum(String.valueOf(30)).readonly(true).build());
        Long deptListId = uidGenerator.getUID();
        menuMap.put(DEPTLIST, deptListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(deptListId).parentId(deptManageId).menuName("部门列表").orderNum(String.valueOf(10)).perms("system:dept:list").readonly(true).build());
        Long deptQueryId = uidGenerator.getUID();
        menuMap.put(DEPTQYERY, deptQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(deptQueryId).parentId(deptManageId).menuName("部门查询").orderNum(String.valueOf(10)).perms("system:dept:query").readonly(true).build());
        Long deptAddId = uidGenerator.getUID();
        menuMap.put(DEPTADD, deptAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(deptAddId).parentId(deptManageId).menuName("部门增加").orderNum(String.valueOf(10)).perms("system:dept:add").readonly(true).build());
        Long deptEditId = uidGenerator.getUID();
        menuMap.put(DEPTEDIT, deptEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(deptEditId).parentId(deptManageId).menuName("部门编辑").orderNum(String.valueOf(10)).perms("system:dept:edit").readonly(true).build());
        Long deptDeleteId = uidGenerator.getUID();
        menuMap.put(DEPTDELETE, deptDeleteId);
        menuList.add(SysMenu.builder().menuType("F").menuId(deptDeleteId).parentId(deptManageId).menuName("部门删除").orderNum(String.valueOf(10)).perms("system:dept:remove").readonly(true).build());

        //岗位管理
        Long postManageId = uidGenerator.getUID();
        menuMap.put(POSTMANAGEID, postManageId);
        menuList.add(SysMenu.builder().menuType("C").menuId(postManageId).parentId(systemManageId).menuName("岗位管理").path("post").component("system/post/index").orderNum(String.valueOf(40)).readonly(true).build());
        Long postListId = uidGenerator.getUID();
        menuMap.put(POSTLIST, postListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(postListId).parentId(postManageId).menuName("岗位列表").orderNum(String.valueOf(10)).perms("system:post:list").readonly(true).build());
        Long postQueryId = uidGenerator.getUID();
        menuMap.put(POSTQYERY, postQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(postQueryId).parentId(postManageId).menuName("岗位查询").orderNum(String.valueOf(10)).perms("system:post:query").readonly(true).build());
        Long postAddId = uidGenerator.getUID();
        menuMap.put(POSTADD, postAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(postAddId).parentId(postManageId).menuName("岗位增加").orderNum(String.valueOf(10)).perms("system:post:add").readonly(true).build());
        Long postEditId = uidGenerator.getUID();
        menuMap.put(POSTEDIT, postEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(postEditId).parentId(postManageId).menuName("岗位编辑").orderNum(String.valueOf(10)).perms("system:post:edit").readonly(true).build());
        Long postDeleteId = uidGenerator.getUID();
        menuMap.put(POSTDELETE, postDeleteId);
        menuList.add(SysMenu.builder().menuType("F").menuId(postDeleteId).parentId(postManageId).menuName("岗位删除").orderNum(String.valueOf(10)).perms("system:post:remove").readonly(true).build());


        //通知公告
        Long noticeManageId = uidGenerator.getUID();
        menuMap.put(NOTICEMANAGEID, userManageId);
        menuList.add(SysMenu.builder().menuType("C").menuId(noticeManageId).parentId(systemManageId).menuName("通知公告").path("notice").component("system/notice/index").orderNum(String.valueOf(50)).readonly(true).build());
        Long noticeListId = uidGenerator.getUID();
        menuMap.put(NOTICELIST, noticeListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(noticeListId).parentId(noticeManageId).menuName("公告列表").orderNum(String.valueOf(10)).perms("system:notice:list").readonly(true).build());
        Long noticeQueryId = uidGenerator.getUID();
        menuMap.put(NOTICEQYERY, noticeQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(noticeQueryId).parentId(noticeManageId).menuName("公告查询").orderNum(String.valueOf(10)).perms("system:notice:query").readonly(true).build());
        Long noticeAddId = uidGenerator.getUID();
        menuMap.put(NOTICEADD, noticeAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(noticeAddId).parentId(noticeManageId).menuName("公告增加").orderNum(String.valueOf(10)).perms("system:notice:add").readonly(true).build());
        Long noticeEditId = uidGenerator.getUID();
        menuMap.put(NOTICEEDIT, noticeEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(noticeEditId).parentId(noticeManageId).menuName("公告编辑").orderNum(String.valueOf(10)).perms("system:notice:edit").readonly(true).build());
        Long noticeDeleteId = uidGenerator.getUID();
        menuMap.put(NOTICEDELETE, noticeDeleteId);
        menuList.add(SysMenu.builder().menuType("F").menuId(noticeDeleteId).parentId(noticeManageId).menuName("公告删除").orderNum(String.valueOf(10)).perms("system:notice:remove").readonly(true).build());


        // 舆情监测

        Long topicListId = uidGenerator.getUID();
        menuMap.put(TOPICLISTID, topicListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(topicListId).parentId(monitoringId).menuName("主题列表").orderNum(String.valueOf(10)).perms("topic:list").readonly(true).build());
        Long topicQueryId = uidGenerator.getUID();
        menuMap.put(TOPICQUERYID, topicQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(topicQueryId).parentId(monitoringId).menuName("主题查询").orderNum(String.valueOf(20)).perms("topic:query").readonly(true).build());
        Long topicDeleteId = uidGenerator.getUID();
        menuMap.put(TOPICDELETEID, topicDeleteId);
        menuList.add(SysMenu.builder().menuType("F").menuId(topicDeleteId).parentId(monitoringId).menuName("主题删除").orderNum(String.valueOf(30)).perms("topic:remove").readonly(true).build());
        Long topicAddId = uidGenerator.getUID();
        menuMap.put(TOPICADDID, topicAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(topicAddId).parentId(monitoringId).menuName("主题创建").orderNum(String.valueOf(40)).perms("topic:add").readonly(true).build());
        Long topicEditId = uidGenerator.getUID();
        menuMap.put(TOPICEDITID, topicEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(topicEditId).parentId(monitoringId).menuName("主题编辑").orderNum(String.valueOf(50)).perms("topic:edit").readonly(true).build());

        //事件分析
        Long eventManageId = uidGenerator.getUID();
        menuMap.put(EVENTMANAGEID, eventManageId);
        menuList.add(SysMenu.builder().menuType("C").menuId(eventManageId).parentId(eventId).menuName("事件查询").path("list").component("event/list/index").orderNum(String.valueOf(10)).readonly(true).build());

        //事件权限
        Long eventListId = uidGenerator.getUID();
        menuMap.put(EVENTLISTID, eventListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(eventListId).parentId(eventManageId).menuName("主题列表").orderNum(String.valueOf(10)).perms("event:list").readonly(true).build());
        Long eventQueryId = uidGenerator.getUID();
        menuMap.put(EVENTQUERYID, eventQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(eventQueryId).parentId(eventManageId).menuName("主题查询").orderNum(String.valueOf(20)).perms("event:query").readonly(true).build());
        Long eventDeleteId = uidGenerator.getUID();
        menuMap.put(EVENTDELETEID, eventDeleteId);
        menuList.add(SysMenu.builder().menuType("F").menuId(eventDeleteId).parentId(eventManageId).menuName("主题删除").orderNum(String.valueOf(30)).perms("event:remove").readonly(true).build());
        Long eventAddId = uidGenerator.getUID();
        menuMap.put(EVENTADDID, eventAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(eventAddId).parentId(eventManageId).menuName("主题创建").orderNum(String.valueOf(40)).perms("event:add").readonly(true).build());
        Long eventEditId = uidGenerator.getUID();
        menuMap.put(EVENTEDITID, eventEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(eventEditId).parentId(eventManageId).menuName("主题编辑").orderNum(String.valueOf(50)).perms("event:edit").readonly(true).build());




        //舆情简报
        Long reportManageId = uidGenerator.getUID();
        menuMap.put(REPORTMANAGEID, reportManageId);
        menuList.add(SysMenu.builder().menuType("C").menuId(reportManageId).parentId(reportId).menuName("简报列表").path("list").component("report/list/index").orderNum(String.valueOf(10)).readonly(true).build());
        Long templateId = uidGenerator.getUID();
        menuMap.put(REPORTTEMPLATE, templateId);
        menuList.add(SysMenu.builder().menuType("C").menuId(templateId).parentId(reportId).menuName("简报模板").path("template").component("report/template/index").orderNum(String.valueOf(20)).readonly(true).build());

        //简报管理权限
        Long reportListId = uidGenerator.getUID();
        menuMap.put(REPORTLISTID, reportListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(reportListId).parentId(reportManageId).menuName("简报列表").orderNum(String.valueOf(10)).perms("system:report:list").readonly(true).build());
        Long reportQueryId = uidGenerator.getUID();
        menuMap.put(REPORTQUERYID, reportQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(reportQueryId).parentId(reportManageId).menuName("简报查询").orderNum(String.valueOf(20)).perms("system:report:query").readonly(true).build());
        Long reportAddId = uidGenerator.getUID();
        menuMap.put(REPORTADDID, reportAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(reportAddId).parentId(reportManageId).menuName("简报新增").orderNum(String.valueOf(30)).perms("system:report:add").readonly(true).build());
        Long reportEditId = uidGenerator.getUID();
        menuMap.put(REPORTEDITID, reportEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(reportEditId).parentId(reportManageId).menuName("简报编辑").orderNum(String.valueOf(40)).perms("system:report:edit").readonly(true).build());
        Long reportRemoveId = uidGenerator.getUID();
        menuMap.put(REPORTREMOVEID, reportRemoveId);
        menuList.add(SysMenu.builder().menuType("F").menuId(reportRemoveId).parentId(reportManageId).menuName("简报删除").orderNum(String.valueOf(50)).perms("system:report:delete").readonly(true).build());

        //模板管理权限
        Long templateListId = uidGenerator.getUID();
        menuMap.put(TEMPLATELISTID, templateListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(templateListId).parentId(templateId).menuName("模板列表").orderNum(String.valueOf(10)).perms("system:template:list").readonly(true).build());
        Long templateQueryId = uidGenerator.getUID();
        menuMap.put(TEMPLATEQUERYID, templateQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(templateQueryId).parentId(templateId).menuName("模板查询").orderNum(String.valueOf(20)).perms("system:template:query").readonly(true).build());
        Long templateAddId = uidGenerator.getUID();
        menuMap.put(TEMPLATEADDID, templateAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(templateAddId).parentId(templateId).menuName("模板新增").orderNum(String.valueOf(30)).perms("system:template:add").readonly(true).build());
        Long templateEditId = uidGenerator.getUID();
        menuMap.put(TEMPLATEEDITID, templateEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(templateEditId).parentId(templateId).menuName("模板编辑").orderNum(String.valueOf(40)).perms("system:template:edit").readonly(true).build());
        Long templateRemoveId = uidGenerator.getUID();
        menuMap.put(TEMPLATEREMOVEID, templateRemoveId);
        menuList.add(SysMenu.builder().menuType("F").menuId(templateRemoveId).parentId(templateId).menuName("模板删除").orderNum(String.valueOf(50)).perms("system:template:delete").readonly(true).build());



        //舆情预警
        Long forecastListId = uidGenerator.getUID();
        menuMap.put(FORECASTLISTID, forecastListId);
        menuList.add(SysMenu.builder().menuType("C").menuId(forecastListId).parentId(forecastId).menuName("预警信息").path("list").component("forecast/list/index").orderNum(String.valueOf(10)).readonly(true).build());
        Long forecastReceiverId = uidGenerator.getUID();
        menuMap.put(FORECASTRECEIVERID, forecastReceiverId);
        menuList.add(SysMenu.builder().menuType("C").menuId(forecastReceiverId).parentId(forecastId).menuName("接收人管理").path("receiver").component("system/receiver/index").orderNum(String.valueOf(20)).readonly(true).build());
        Long forecastRuleId = uidGenerator.getUID();
        menuMap.put(FORECASTRULEID, forecastRuleId);
        menuList.add(SysMenu.builder().menuType("C").menuId(forecastRuleId).parentId(forecastId).menuName("预警设置").path("rule").component("forecast/rule/index").orderNum(String.valueOf(30)).readonly(true).build());

        //预警规则权限
        Long ruleListId = uidGenerator.getUID();
        menuMap.put(RULELISTID, ruleListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(ruleListId).parentId(forecastId).menuName("预警规则列表").orderNum(String.valueOf(10)).perms("system:rule:list").readonly(true).build());
        Long ruleAddId = uidGenerator.getUID();
        menuMap.put(RULEADDID, ruleAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(ruleAddId).parentId(forecastId).menuName("预警规则新增").orderNum(String.valueOf(20)).perms("system:rule:add").readonly(true).build());
        Long ruleEditId = uidGenerator.getUID();
        menuMap.put(RULEEDITID, ruleEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(ruleEditId).parentId(forecastId).menuName("预警规则编辑").orderNum(String.valueOf(30)).perms("system:rule:edit").readonly(true).build());
        Long ruleQueryId = uidGenerator.getUID();
        menuMap.put(RULEQUERYID, ruleQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(ruleQueryId).parentId(forecastId).menuName("预警规则查询").orderNum(String.valueOf(40)).perms("system:rule:query").readonly(true).build());
        Long ruleRemoveId = uidGenerator.getUID();
        menuMap.put(RULEREMOVEID, ruleRemoveId);
        menuList.add(SysMenu.builder().menuType("F").menuId(ruleRemoveId).parentId(forecastId).menuName("预警规则删除").orderNum(String.valueOf(50)).perms("system:rule:remove").readonly(true).build());

        //接收人权限
        Long receiverListId = uidGenerator.getUID();
        menuMap.put(RECEIVERLISTID, receiverListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(receiverListId).parentId(forecastId).menuName("预警接收人列表").orderNum(String.valueOf(10)).perms("system:receiver:list").readonly(true).build());
        Long receiverAddId = uidGenerator.getUID();
        menuMap.put(RECEIVERADDID, receiverAddId);
        menuList.add(SysMenu.builder().menuType("F").menuId(receiverAddId).parentId(forecastId).menuName("预警接收人新增").orderNum(String.valueOf(20)).perms("system:receiver:add").readonly(true).build());
        Long receiverEditId = uidGenerator.getUID();
        menuMap.put(RECEIVEREDITID, receiverEditId);
        menuList.add(SysMenu.builder().menuType("F").menuId(receiverEditId).parentId(forecastId).menuName("预警接收人编辑").orderNum(String.valueOf(30)).perms("system:receiver:edit").readonly(true).build());
        Long receiverQueryId = uidGenerator.getUID();
        menuMap.put(RECEIVERQUERYID, receiverQueryId);
        menuList.add(SysMenu.builder().menuType("F").menuId(receiverQueryId).parentId(forecastId).menuName("预警接收人查询").orderNum(String.valueOf(40)).perms("system:receiver:query").readonly(true).build());
        Long receiverRemoveId = uidGenerator.getUID();
        menuMap.put(RECEIVERREMOVEID, receiverRemoveId);
        menuList.add(SysMenu.builder().menuType("F").menuId(receiverRemoveId).parentId(forecastId).menuName("预警接收人删除").orderNum(String.valueOf(50)).perms("system:receiver:remove").readonly(true).build());

        //案例管理
        Long caseListId = uidGenerator.getUID();
        menuMap.put(CASELIST, caseListId);
        menuList.add(SysMenu.builder().menuType("F").menuId(caseListId).parentId(caseId).menuName("案例查询").orderNum(String.valueOf(10)).perms("case:list").readonly(true).build());



        return menuService.saveBatch(menuList)>0;

    }

    @Override
    public boolean reset(String tenant) {
        //TODO 待实现
        // 1，清空所有表的数据
        // 2，重新初始化 tenant
        // 3，重新初始化 业务数据
        //        init(tenant);
        return true;
    }

    @Override
    public int delete(List<Long> ids, List<String> tenantCodeList) {
        // 清空所有表中当前租户的数据
        //TODO 待实现
        // 1,查询系统中的所有表
        // 删除该租户的所有数据
//        查找租户下的所有用户
        List<Long> userIds=userService.selectUserByTenantCodes(tenantCodeList);
        //删除用户
        if(userIds.size()>0){
            userService.deleteUserByIds(userIds.toArray(new Long[userIds.size()]),true);
        }
        //删除角色
        roleService.deleteRoleByTenantCode(tenantCodeList);
        //删除角色菜单关联表
        roleService.deleteRoleMenuByTenantCode(tenantCodeList);
        //删除岗位
        postService.deletePostByTenantCode(tenantCodeList);
        //删除部门
        deptService.deleteDeptByTenantCode(tenantCodeList);
        //删除公告
        noticeService.deleteNoticeByTenantCode(tenantCodeList);
        //删除菜单
        menuService.deleteMenuByTenantCode(tenantCodeList);
        return 1;
    }
}
